package yl.hs.bmipfull.service.production.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.production.IProductionPackageDao;
import yl.hs.bmipfull.daos.production.IProductionPackageUserDao;
import yl.hs.bmipfull.pojo.production.ProductionPackageEntity;
import yl.hs.bmipfull.pojo.production.ProductionPackageUserEntity;
import yl.hs.bmipfull.service.production.IProductionPackageUserService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作用:  服务层接口实现<br/>
 * 创建时间: 2023年07月20日 09时27分12秒 <br/>
 *
 * @author 吴高丰
 */
@Service
public class ProductionPackageUserServiceImpl extends ProductionBaseService implements IProductionPackageUserService {

    @Autowired
    IProductionPackageUserDao dao;
    @Autowired
    IProductionPackageDao packageDao;

    @Override
    public List<ProductionPackageUserEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductionPackageUserEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<Object> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        var user = HsApp.getCurrent();
        if(user!=null){
            pageQuery.getQuery().put("types", user.getType()==null?0:user.getType());
        }
        var totil = dao.totilQuantity(pageQuery.getQuery());
        pageQuery.checkSortSides(ProductionPackageUserEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<Object> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        var data = dao.queryPage(pageQuery.getQuery());
        List<Object> datas = new ArrayList<>();
        data.forEach(s->{
            var map = s.toMap();
            var pack = packageDao.findBySN(s.getPackSN());
            if(pack!=null){
                map.put("productCateName",pack.getProductCateName());
                map.put("productName",pack.getProductName());
                map.put("productSpec",pack.getProductSpec());
                map.put("packageName",pack.getPackageName());
                map.put("packUnit",pack.getPackUnit());
                map.put("packWay",pack.getPackWay());
                map.put("packWeight",pack.getPackWeight());
                map.put("image",pack.getImage());
            }
            datas.add(map);
        });
        result.setObject(totil);
        result.setData(datas);
        return result;
    }


    @Override
    public ProductionPackageUserEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveproduction_package_user(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ProductionPackageUserEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new ProductionPackageUserEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ProductionPackageUserEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                if (entity.getType() == 1) {
                    var page = packageDao.findBySN(entity.getPackSN());
                    if (page != null) {
                        packageDao.udateQuantity(entity.getPackSN(), page.getQuantity() + entity.getQuantity());
                        writeLog(ActionType.UPDATE, entity.getPackSN(), "插入入库记录,库存由"+page.getQuantity()+"变为"+(page.getQuantity() + entity.getQuantity()), Tl.toJson(entity), ProductionPackageEntity.class);
                        writeLog(ActionType.CREATE, entity.getSn(), "插入入库记录", Tl.toJson(entity), ProductionPackageEntity.class);
                    }
                } else if(entity.getType() == 2){
                    var page = packageDao.findBySN(entity.getPackSN());
                    if (page != null) {
                        packageDao.udateQuantity(entity.getPackSN(), page.getQuantity() - entity.getQuantity());
                        writeLog(ActionType.UPDATE, entity.getPackSN(), "插入领用记录,库存由"+page.getQuantity()+"变为"+(page.getQuantity() - entity.getQuantity()), Tl.toJson(entity), ProductionPackageEntity.class);
                        writeLog(ActionType.CREATE, entity.getSn(), "插入领用记录", Tl.toJson(entity), ProductionPackageEntity.class);
                    }
                }else{
                    var page = packageDao.findBySN(entity.getPackSN());
                    if (page != null) {
                        packageDao.udateQuantity(entity.getPackSN(), page.getQuantity() + entity.getQuantity());
                        writeLog(ActionType.UPDATE, entity.getPackSN(), "插入退库记录,库存由"+page.getQuantity()+"变为"+(page.getQuantity() + entity.getQuantity()), Tl.toJson(entity), ProductionPackageEntity.class);
                        writeLog(ActionType.CREATE, entity.getSn(), "插入退库记录", Tl.toJson(entity), ProductionPackageEntity.class);
                    }
                }
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新信息", Tl.toJson(entity), ProductionPackageUserEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        var entity = dao.findBySN(sn);
        if (result > 0) {
            if (entity.getType() == 1) {
                var page = packageDao.findBySN(entity.getPackSN());
                if (page != null) {
                    packageDao.udateQuantity(entity.getPackSN(), page.getQuantity() - entity.getQuantity());
                    writeLog(ActionType.UPDATE, entity.getPackSN(), "删除入库记录，库存由"+page.getQuantity()+"变为"+(page.getQuantity() - entity.getQuantity()), Tl.toJson(entity), ProductionPackageEntity.class);
                }
            } else if(entity.getType()==2){
                var page = packageDao.findBySN(entity.getPackSN());
                if (page != null) {
                    packageDao.udateQuantity(entity.getPackSN(), page.getQuantity() + entity.getQuantity());
                    writeLog(ActionType.UPDATE, entity.getPackSN(), "删除出库记录，库存由"+page.getQuantity()+"变为"+(page.getQuantity() + entity.getQuantity()), Tl.toJson(entity), ProductionPackageEntity.class);
                }
            }else{
                var page = packageDao.findBySN(entity.getPackSN());
                if (page != null) {
                    packageDao.udateQuantity(entity.getPackSN(), page.getQuantity() - entity.getQuantity());
                    writeLog(ActionType.UPDATE, entity.getPackSN(), "删除退库记录，库存由"+page.getQuantity()+"变为"+(page.getQuantity() - entity.getQuantity()), Tl.toJson(entity), ProductionPackageEntity.class);
                }
            }
        }
        return result;
    }

    @Override
    public List<ProductionPackageUserEntity> queryByPackSNList(String packSN) {
        return dao.queryByPackSNList(packSN);
    }
}